package master;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

/**
 * @author chenyw
 * @date 2022/8/27 21:51
 */
public class M2170minimumOperations {
    public int minimumOperations(int[] nums) {
        Map<Integer, Integer> mapA = new HashMap<Integer, Integer>();
        Map<Integer, Integer> mapB = new HashMap<Integer, Integer>();

        for (int i = 0; i < nums.length; i += 2) {
            if (i + 1 < nums.length) {
                mapB.put(nums[i + 1], mapB.getOrDefault(nums[i + 1], 0) + 1);
            }
            mapA.put(nums[i], mapA.getOrDefault(nums[i], 0) + 1);
        }

        //分别记录奇偶位的最大值和次大值
        int aa = 0, a1 = 0, a2 = 0;
        for (Map.Entry<Integer, Integer> entry : mapA.entrySet()) {
            if (entry.getValue() > a1) {
                a2 = a1;
                a1 = entry.getValue();
                aa = entry.getKey();
            } else if (entry.getValue() > a2) {
                a2 = entry.getValue();
            }
        }
        int bb = 0, b1 = 0, b2 = 2;
        for (Map.Entry<Integer, Integer> entry : mapB.entrySet()) {
            if (entry.getValue() > b1) {
                b2 = b1;
                b1 = entry.getValue();
                bb = entry.getKey();
            } else if(entry.getValue() > b2) {
                b2 = entry.getValue();
            }
        }
        if (aa != bb) {
            return nums.length - a1 - b1;
        } else {
            return nums.length - Math.max(a1 + b2, b1 + a2);
        }
    }
}
